package tests.db;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.ArrayList;

import struct.*;
import db.DBManager;

/**
 * Tests Adults (tables include: Adults, In_Relationship_With),
 *  Dependents (tables include: Dependents, Lives_With),
 *  and Ministry (tables include: Ministries) related methods
 * @author Gabrielle DuBois
 */
public class DBManagerTest {
    private DBManager dbm;

    private final String a1First = "Oris";
    private final String last = "Pothier";
    private final String a1DOB = "03/03/1963";
    private final String joined = "03/03/2003";
    private final String a1Occup = "Trainer";
    private final String a1Emp = "Gold Gym";
    private final String phone = "920.000.0000";
    private final String a1Email = "oris@email.com";
    private final String notes = "";
    private final String a2First = "Dasiy";
    private final String a2DOB = "04/04/1963";
    private final String a2Occup = "Weaver";
    private final String a2Emp = "Self-Employed";
    private final String a2Email = "dasiy@email.com";
    private final int numAttributes = 10;   //number of attributes that compose an adult object
    private final String a3First = "Octavian";
    private final String a3Last = "Betties";
    private final String a3DOB = "03/03/1960";
    private final String a3Occup = "Slacker";
    private final String a3Emp = "Everywhere";
    private final String a3Email = "octavian@email.com";

    private final Adult a1 = new Adult(a1First, last, a1DOB, joined, a1Occup, a1Emp, phone, phone, a1Email, notes);
    private final Adult a2 = new Adult(a2First, last, a2DOB, joined, a2Occup, a2Emp, phone, phone, a2Email, notes);
    private final Adult a3 = new Adult(a3First, a3Last, a3DOB, joined, a3Occup, a3Emp, phone, phone, a3Email, notes);
    private Adult[] a;


    @Before
    public void setUp() throws Exception{
        //todo before each @Test
        dbm = new DBManager();
    }

   @After
    public void tearDown() throws Exception {
        //todo after each @Test
        dbm.close();
    }

   @AfterClass
   public static void tearDownAll() throws Exception{
        

    }

    @Test
    public void testGetIndividualAdult() throws Exception{
        boolean success = false;

        DBObject person = dbm.getIndividual("Joe", "Barnier", "12/31/1985");
        if (person != null){
            String[] p = (String[]) person.getAttributes();
            // email should be in 8th position
            success =  p[8].equalsIgnoreCase("joe.barnier@email.com") ;
        }else{
            success = false;
        }

        assertTrue(success);

        person = dbm.getIndividual("Gabrielle", "DuBois", "08/23/1983");
        if (person == null){
            //person was not found which is the goal
            success = true;
        }else{
            success = false;
        }

        dbm.close();
        assertTrue(success);
    }

    @Test
    public void testGetIndividualDependent() throws Exception{
        boolean success = false;

        DBObject person = dbm.getIndividual("Eston", "Barnier", "06/16/2017");
        if (person != null){
            String[] p = (String[]) person.getAttributes();
            // school should be in 3rd position
            success =  p[3].equalsIgnoreCase("Roosevelt Elementary") ;
        }else{
            success = false;
        }

        assertTrue(success);
        dbm.close();
    }

    /**
     * Adds a Ministry and then cleans up after itself by deleting it.
     * @throws Exception
     */
    @Test
    public void testAddMinistry() throws Exception{
        boolean success = true;
        String newMinistry = "Plane Riding";
        ArrayList<String> names = dbm.getMinistryNames(true);

        for (int i=0;i<names.size();i++){
            if ( names.get(i).equalsIgnoreCase(newMinistry) ){
                //name already exists
                success = false;
                break;
            }
        }
        assertTrue(success);
        success = false;

        dbm.addMinistry(newMinistry,  false, "");
        names = dbm.getMinistryNames(true);

        for (int i=0;i<names.size();i++){
            if ( names.get(i).equalsIgnoreCase(newMinistry) ){
                //name now exists!
                success = true;
                break;
            }
        }
        assertTrue(success);

        dbm.dM(newMinistry);
        names = dbm.getMinistryNames(true);
        success = true;

        for (int i=0;i<names.size();i++){
            if ( names.get(i).equalsIgnoreCase(newMinistry) ){
                //name still exists
                success = false;
                break;
            }
        }
        assertTrue(success);
    }

    @Test
    public void testDeactivateMinistry01() throws Exception{
        boolean success = false;
        String newMinistry = "Food Kitchen";

        //Add new ministry just in case
        dbm.addMinistry(newMinistry, true, "");
        ArrayList<String> names = dbm.getMinistryNames(true);

        for (int i=0;i<names.size();i++){
            if ( names.get(i).equalsIgnoreCase(newMinistry) ){
                //name exists which means its active to start with
                success = true;
                break;
            }
        }
        assertTrue(success);

    }

    @Test
    public void testDeactivateMinistry02() throws Exception{
        boolean success = true;
        String newMinistry = "Food Kitchen";

        dbm.toggleMinistryActivationStatus(newMinistry, false);
        ArrayList<String> names = dbm.getMinistryNames(true);

        for (int i=0;i<names.size();i++){
            if ( names.get(i).equalsIgnoreCase(newMinistry) ){
                //name exists which means its still active which is bad :(
                success = false;
                break;
            }
        }
        assertTrue(success);

        //delete added ministry
        dbm.dM(newMinistry);
    }

    @Test
    public void testGetMinistryNames(){
        boolean success = false;
        ArrayList<String> names = dbm.getMinistryNames(true);
        ArrayList<String> dNames = dbm.getMinistryNames(false);

        int size = names.size();
        int dSize = dNames.size();
        System.out.println("Number of Current Active Ministries: " + size);

        if (size > 0){
            success = true;

            for (int i=0; i<size; i++){
                System.out.println(names.get(i));
            }//for( i)
        }

        System.out.println("Number of  Deactive Ministries: " + dSize);
        for (int i=0; i<dSize; i++){
            System.out.println(dNames.get(i));
        }//for( i)

        assertTrue(success);
    }

    @Test
    public void testGetMinistry(){
        boolean success = false;
        String name = "Bunfest";
        String[] m = dbm.getMinistry(name);

        System.out.println("");
        System.out.println("Attributes of Ministry: " + name);

        for (int i=0; i<m.length; i++){
            System.out.print("'" + m[i] + "' ");
            success = true;
        }//for(i)
        System.out.println("");

        assertTrue(success);
    }//testGetMinistry()

    @Test
    public void testDoesExistAdult(){
        String[] akey = {"Joe", "Barnier","12/31/1985"};
        boolean works = dbm.doesExist("adults", akey);

        assertTrue(works);
    }

    @Test
    public void testDoesExistDependent(){
        String[] akey = {"Eston", "Barnier","06/16/2017"};
        boolean works = dbm.doesExist("dependents", akey);

        assertTrue(works);
    }

    @Test
    public void testDoesExistFamily(){
        String[] akey = {"DuBois", "(920)555-5555"};
        boolean works = dbm.doesExist("families", akey);

        assertTrue(works);
    }

    @Test
    public void testDoesExistMinistry(){
        String[] akey = {"Speaker"};
        boolean works = dbm.doesExist("ministries", akey);

        assertTrue(works);
    }

    @Test
    public void testDoesNOTExistAdult(){
        String[] akey = {"Barbie", "Martell","01/01/1801"};
        boolean works = dbm.doesExist("adults", akey);

        assertFalse(works);
    }

    @Test
    public void testDoesNOTExistDependent(){
        String[] akey = {"Lola", "Steward","12/31/2000"};
        boolean works = dbm.doesExist("dependents", akey);

        assertFalse(works);
    }

    @Test
    public void testDoesNOTExistFamily(){
        String[] akey = {"Washington", "(920)333-3333"};
        boolean works = dbm.doesExist("families", akey);

        assertFalse(works);
    }

    @Test
    public void testDoesNOTExistMinistry(){
        String[] akey = {"Food Tasting"};
        boolean works = dbm.doesExist("ministries", akey);

        assertFalse(works);
    }


}//DBManagerTest
